# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 客户端：WebSocket.py
# @Author: dongguangwen
# @Date  : 2025-01-05 10:38
import asyncio
import websockets


async def test_client():
    uri = "ws://127.0.0.1:6789"  # 连接到 WebSocket 服务器的地址
    async with websockets.connect(uri) as websocket:
        while True:
            message = input("Enter message to send (or 'exit' to quit): ")
            if message.lower() == 'exit':
                print("Closing connection...")
                break
            await websocket.send(message)  # 发送消息到服务器
            response = await websocket.recv()  # 接收服务器的响应
            print(f"Received from server: {response}")


# 运行客户端
asyncio.run(test_client())
